package cn.edu.gcu.giftems.service;

import cn.edu.gcu.giftems.daos.OrderDao;
import cn.edu.gcu.giftems.pojos.Order;
import cn.edu.gcu.giftems.utils.PageUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * TODO: Description here...
 *
 * @author ZhengHQ
 * @create 2018-09-24
 * @see
 */
@Transactional
public class OrderService {

    private OrderDao orderDao = new OrderDao();

    public OrderDao getOrderDao() {
        return orderDao;
    }

    public void setOrderDao(OrderDao orderDao) {
        this.orderDao = orderDao;
    }

    public void save(Order order) {
        orderDao.save(order);
    }

    //根据订单编号查询订单信息
    public Order findById(int id) {
        return orderDao.finById(id);
    }

    //更新订单信息
    public void update(Order order) {
        orderDao.update(order);
    }

    //根据订单id删除订单
    public void delete(Order order) {
        orderDao.delete(order);
    }

    public int findCount() {
        return orderDao.findCount();
    }


    //分页查询
    public PageUtils<Order> findByPage(int currPage) {
        PageUtils<Order> PageUtils = new PageUtils<Order>();
        //封装当前的页数
        PageUtils.setCurrPage(currPage);
        //封装每页记录数
        int pageSize = 10;
        PageUtils.setPageSize(pageSize);
        //封装总记录数
        int totalCount = orderDao.findCount();
        PageUtils.setTotalCount(totalCount);
        //封装总页数
        int totalPage = totalCount%pageSize == 0?totalCount/pageSize:(totalCount/pageSize+1);
        PageUtils.setTotalPage(totalPage);
        //封装每页显示的数据
        int begin = (currPage - 1)*pageSize;
        List<Order> list = orderDao.findByPage(begin,pageSize);
        PageUtils.setList(list);
        return PageUtils;
    }

    public List<Order> findAll() {
        return orderDao.findAll();
    }

    public PageUtils<Order> findByParameter(Integer currPage, String filter_name, String simple_filter_value) {
        PageUtils<Order> PageUtils = new PageUtils<Order>();
        PageUtils.setCurrPage(currPage);
        PageUtils.setTotalPage(1);
        List<Order> list = orderDao.findByParameter(filter_name,simple_filter_value);
        PageUtils.setList(list);
        int totalCount = list.size();
        PageUtils.setTotalCount(totalCount);
        return PageUtils;
    }

    public PageUtils<Order> findByTime(Integer currPage, String begin_time, String finish_time) {
        PageUtils<Order> PageUtils = new PageUtils<Order>();
        PageUtils.setCurrPage(currPage);
        PageUtils.setTotalPage(1);
        List<Order> list = orderDao.findByTime(begin_time, finish_time);
        PageUtils.setList(list);
        int totalCount = list.size();
        PageUtils.setTotalCount(totalCount);
        return PageUtils;
    }
}
